/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.UserManagement;

import Utilities.PermissionsConstants;

/**
 * Instances of this class are used by the user interface when a request to reset
 * a user's password is sent to the users management service(s).
 *
 * @author Angel Sanadinov
 */
public class UserPasswordResetRequest extends UserManagementRequest
{
    private String newPassword; //the new user password
    private String oldPassword; //the old user password *
    //* only set when a user requests to reset his/her password

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public UserPasswordResetRequest()
    {
        this.newPassword = null;
        this.oldPassword = null;
    }

    /**
     * Constructs the request object using the supplied data for a password reset.
     *
     * @param requestorId the id of the user that requested the action
     * @param affectedUserId the id of the affected user by the reset
     * @param newPassword the new user password
     *
     * @see UserPasswordResetRequest#UserPasswordResetRequest(int, java.lang.String, java.lang.String)
     *      Reset requestor's password
     */
    public UserPasswordResetRequest(int requestorId, int affectedUserId, String newPassword)
    {
        super.setRequestorId(requestorId);
        super.setAffectedUserId(affectedUserId);
        super.setRequestedAction(PermissionsConstants.ACTION_USER_RESET_PASSWORD);
        this.newPassword = newPassword;
    }

    /**
     * Constructs the request object using the supplied data for a password reset.
     * <br><br>
     * <b>Note:</b> <i>This method will reset the password of the user that requested
     *              the action.</i>
     *
     * @param requestorId the id of the user that requested the action
     * @param oldPassword the old user password
     * @param newPassword the new user password
     *
     * @see UserPasswordResetRequest#UserPasswordResetRequest(int, int, java.lang.String)
     *      Reset another user's password
     */
    public UserPasswordResetRequest(int requestorId, String oldPassword, String newPassword)
    {
        super.setRequestorId(requestorId);
        super.setRequestedAction(PermissionsConstants.ACTION_USER_RESET_OWN_PASSWORD);
        this.oldPassword = oldPassword;
        this.newPassword = newPassword;
    }

    /**
     * Retrieves the new user password.
     *
     * @return the new user password
     */
    public String getNewPassword()
    {
        return newPassword;
    }

    /**
     * Retrieves the old user password, if available.
     *
     * @return the old user password
     */
    public String getOldPassword()
    {
        return oldPassword;
    }

    /**
     * Sets the new user password.
     *
     * @param newPassword the new user password
     */
    public void setNewPassword(String newPassword)
    {
        this.newPassword = newPassword;
    }

    /**
     * Sets the old user password.
     *
     * @param oldPassword the old user password
     */
    public void setOldPassword(String oldPassword)
    {
        this.oldPassword = oldPassword;
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * This object is valid only when all data required for the specified request is valid.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        boolean result = false;

        //for a regular password reset, the affected user id, the new password and
        //a requestor id are needed; note: the affected user cannot be the requestor!
        if(getRequestedAction() == PermissionsConstants.ACTION_USER_RESET_PASSWORD)
        {
            if(isAffectedUserIdValid() && newPassword != null && !newPassword.trim().equals("")
                    && isRequestorIdValid() && getAffectedUserId() != getRequestorId())
                result = true;
            else
                result = false;
        }
        else if(getRequestedAction() == PermissionsConstants.ACTION_USER_RESET_OWN_PASSWORD)
        {//for own password reset, the old password, the new password and a valid user id are needed
            if(oldPassword != null && newPassword != null && !newPassword.trim().equals("")
                    && isRequestorIdValid())
                result = true;
            else
                result = false;
        }
        else
            result = false;

        return result;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (affectedUserId)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return UserPasswordResetRequest.class.getSimpleName()
                + ": (" + getAffectedUserId() + ")";
    }
}
